package com.wan37.logic.fighting.hand;

import com.wan37.logic.domain.Result;
import com.wan37.logic.domain.vo.*;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.skill.impl.SkillManager;
import com.wan37.logic.utils.DelayDequeThreadPollUtil;
import com.wan37.protobuf.constant.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-06-28 12:12
 **/
@Component
public class PlayerAttackPlayer implements Fighting {
    private DelayDequeThreadPollUtil delayDequeThreadPollUtil;
    private SceneServiceCache sceneServiceCache;
    private RoleServiceCache roleServiceCache;
    private UserServiceCache userServiceCache;
    private SkillManager skillManager;

    @Override
    public Result start(FightingVO fightingVO) {
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.skillManager = SpringContextHolder.getBean("skillManager");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");

        Map<String, SkillVO> skillVOMap = roleServiceCache.getRoleServiceMap().get(fightingVO.getAllRoleId()).getSkillVOMap();
        if (skillVOMap.isEmpty()) {
            return new Result(CommonFighting.ATTACK_ERROR_NOT_ANY_SKILL, "角色没有任何技能，无法攻击!!!");

        }
        if (roleServiceCache.getRoleServiceMap().get(fightingVO.getAllRoleId())
                .getRoleVO().getStatus() == AllRoleLabel.STATUS_DIE) {
            return new Result(CommonFighting.ATTACK_ERROR_IS_DIE, "角色已经死亡，无法攻击!!!");
        }
        SkillVO skillVO = skillVOMap.get(fightingVO.getSkillId());
        AllRoleVO allRoleVO = roleServiceCache.getRoleServiceMap().get(fightingVO.getAllRoleId()).getRoleVO();
        List<String> allRoleIdList = fightingVO.getAllRoleIdList();
        List<AllRoleVO> allRoleVOList = new ArrayList<>();
        for (String allRoleId : allRoleIdList) {
            allRoleVOList.add(roleServiceCache.getRoleServiceMap().get(allRoleId).getRoleVO());
        }
        /**
         * 判断技能对自己用还是对敌方使用，根据选择不同进行技能生效对象选定
         */
        for (String userId : sceneServiceCache.getSceneServiceMap().get(fightingVO.getSceneId()).getUserIdSet()) {
            if (skillVO.getLabel().startsWith(SkillLabel.AOE + SkillLabel.FRIEND)){
                if (userId.equals(fightingVO.getUserId())){
                    for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()) {
                        if (!allRoleIdList.contains(roleId)) {
                            RoleVO roleVO = roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO();
                            if (roleVO.getStatus() != AllRoleLabel.STATUS_DIE) {
                                allRoleVOList.add(roleVO);
                            }
                        }
                    }
                    break;
                }
            }else if (skillVO.getLabel().startsWith(SkillLabel.AOE + SkillLabel.ENEMY)){
                if (!userId.equals(fightingVO.getUserId())){
                    for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()) {
                        if (!allRoleIdList.contains(roleId)) {
                            RoleVO roleVO = roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO();
                            if (roleVO.getStatus() != AllRoleLabel.STATUS_DIE) {
                                allRoleVOList.add(roleVO);
                            }
                        }
                    }
                }
            }
        }
        if (skillVO == null) {

            return new Result(CommonFighting.ATTACK_ERROR_NOT_SKILL, "角色没有该技能！！");

        } else if (allRoleVO == null || allRoleIdList == null) {

            return new Result(CommonFighting.ATTACK_ERROR_NOT_ENTITY, "目标不存在！！！");
        }
        /**
         * 能够进入这里的一定是临时场景，因为客户端传入的是sceneName，只有临时场景sceneId == sceneName
         */
        else if (sceneServiceCache.getSceneServiceMap().containsKey(fightingVO.getSceneId())) {
            if (System.currentTimeMillis() - sceneServiceCache
                    .getSceneServiceMap().get(fightingVO.getSceneId()).getCreatTime()
                    > BossSceneCommon.MAX_FIGHTING_TIME) {
                return new Result(CommonFighting.GAME_FIGHTING_TIMEOUT, "战斗超时！！用户挑战失败");
            }
        }
        //计算消耗
        return cost(skillVO, allRoleVO, allRoleVOList);
    }

    @Override
    public Result cost(SkillVO skillVO, AllRoleVO allRoleVO, List<AllRoleVO> allRoleVOList) {
        //检验角色剩余HP是否能够释放该技能。
        if (allRoleVO.getNumberMap().get(CommonBase.HP) - skillVO.getCostHP() < 0) {
            return new Result(CommonFighting.ATTACK_ERROR_NOT_HP, "剩余蓝量不允许释放当前技能！！！");
        }
        //判断场景中敌方角色是否全部死亡
        if (allRoleVOList.isEmpty()) {
            return new Result(CommonFighting.ATTACK_ERROR_ALL_ROLE_DIE, "敌方角色已被击杀！！！");
        }

        Map<String, Map<String, Long>> allRoleVONumberMap = null;

        if (skillVO.getLabel().startsWith(SkillLabel.AOE)) {
            //技能释放处理双方数值
            allRoleVONumberMap = skillManager.handler(skillVO, allRoleVO, allRoleVOList);
        } else if (skillVO.getLabel().startsWith(SkillLabel.ONE)) {
            //技能释放处理双方数值
            allRoleVONumberMap = skillManager.handler(skillVO, allRoleVO, allRoleVOList.get(0));
        }

        Result result = finish(skillVO, allRoleVO, allRoleVOList);
        //后续结果处理
        return new SuccessFightingResult(result.getCode(), result.getContent(), allRoleVONumberMap);

    }

    @Override
    public Result finish(SkillVO skillVO, AllRoleVO allRoleVO, List<AllRoleVO> allRoleVOList) {
        this.delayDequeThreadPollUtil = SpringContextHolder.getBean(("delayDequeThreadPollUtil"));
        for (AllRoleVO allRoleVO2 : allRoleVOList) {
            if (allRoleVO2.getNumberMap().get(CommonBase.MP) <= 0) {
                //角色状态设置为0，代表角色已死亡
                allRoleVO2.setStatus(AllRoleLabel.STATUS_DIE);
//                delayDequeThreadPollUtil.updateTimeSchedule(20, allRoleVO.getAllRoleId(),
//                        monsterVO.getSceneId(), new RevertMonsterHandler(allRoleVO2.getAllRoleId()));
            }
        }
        return new Result(CommonFighting.ATTACK_SUCCESS, "");
    }
}
